package edu.westga.vocalengineer.audio;

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;


public class AudioDataRecorder implements Runnable {
	
	private AudioFormat format;
	private DataLine.Info info;
	private TargetDataLine targetDataLine;
	private AudioInputStream inputStream;
	private byte[] data;
	
	private boolean stopRequested;
	
	public AudioDataRecorder() {
		format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
								44100.0F, 16, 2, 4, 44100.0F, false);
		info = new DataLine.Info(TargetDataLine.class, format);
		stopRequested = false;
	}
	
	public void startRecording() throws LineUnavailableException, IOException {
		data = new byte[128000]; 
		targetDataLine = (TargetDataLine)AudioSystem.getLine(info);
		targetDataLine.start();
		inputStream = new AudioInputStream(targetDataLine);
		inputStream.read(data, 0, data.length);
	}
	
	public void stopRecording() {
		targetDataLine.stop();
		targetDataLine.close();
	}
	
	public byte[] getData() {
		return data;
	}
	
	public void run() { }
	
	public AudioData getAudioData() {
		return new AudioData(data, format);
	}

}
